package com.mdd.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.yulichang.query.MPJQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.admin.validate.commons.PageValidate;
import com.mdd.admin.service.IFlowNodeService;
import com.mdd.admin.validate.FlowNodeCreateValidate;
import com.mdd.admin.validate.FlowNodeUpdateValidate;
import com.mdd.admin.validate.FlowNodeSearchValidate;
import com.mdd.admin.vo.FlowNodeListedVo;
import com.mdd.admin.vo.FlowNodeDetailVo;
import com.mdd.common.config.GlobalConfig;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.FlowNode;
import com.mdd.common.mapper.FlowNodeMapper;
import com.mdd.common.util.ListUtils;
import com.mdd.common.util.TimeUtils;
import com.mdd.common.util.UrlUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.*;

/**
 * 流程节点关联实现类
 * @author LikeAdmin
 */
@Service
public class FlowNodeServiceImpl implements IFlowNodeService {
        
    @Resource
    FlowNodeMapper flowNodeMapper;

    /**
     * 流程节点关联列表
     *
     * @author LikeAdmin
     * @param pageValidate 分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<FlowNodeListedVo>
     */
    @Override
    public PageResult<FlowNodeListedVo> list(PageValidate pageValidate, FlowNodeSearchValidate searchValidate) {
        Integer page  = pageValidate.getPageNo();
        Integer limit = pageValidate.getPageSize();

        QueryWrapper<FlowNode> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", 0);
        queryWrapper.orderByDesc("id");

        flowNodeMapper.setSearch(queryWrapper, searchValidate, new String[]{
            "=:id:int",
            "=:isValid@is_valid:str",
            "=:proposalType@proposal_type:str",
            "=:isFlow@is_flow:str",
            "=:isBackNode@is_back_node:str",
            "=:userId@user_id:int",
            "like:userName@user_name:str",
            "=:updateUserId@update_user_id:int",
            "like:updateUserName@update_user_name:str",
        });

        IPage<FlowNode> iPage = flowNodeMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<FlowNodeListedVo> list = new LinkedList<>();
        for(FlowNode item : iPage.getRecords()) {
            FlowNodeListedVo vo = new FlowNodeListedVo();
            BeanUtils.copyProperties(item, vo);
            vo.setCreateTime(TimeUtils.timestampToDate(item.getCreateTime()));
            vo.setUpdateTime(TimeUtils.timestampToDate(item.getUpdateTime()));
            list.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    /**
     * 流程节点关联详情
     *
     * @author LikeAdmin
     * @param id 主键参数
     * @return FlowNode
     */
    @Override
    public FlowNodeDetailVo detail(Integer id) {
        FlowNode model = flowNodeMapper.selectOne(
                new QueryWrapper<FlowNode>()
                    .eq("id", id)
                    .eq("is_delete", 0)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        FlowNodeDetailVo vo = new FlowNodeDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 流程节点关联新增
     *
     * @author LikeAdmin
     * @param createValidate 参数
     */
    @Override
    public void add(FlowNodeCreateValidate createValidate) {
        FlowNode model = new FlowNode();
        model.setIsValid(createValidate.getIsValid());
        model.setProposalType(createValidate.getProposalType());
        model.setIsFlow(createValidate.getIsFlow());
        model.setIsBackNode(createValidate.getIsBackNode());
        model.setUserId(createValidate.getUserId());
        model.setUserName(createValidate.getUserName());
        model.setUpdateUserId(createValidate.getUpdateUserId());
        model.setUpdateUserName(createValidate.getUpdateUserName());
        flowNodeMapper.insert(model);
    }

    /**
     * 流程节点关联编辑
     *
     * @author LikeAdmin
     * @param updateValidate 参数
     */
    @Override
    public void edit(FlowNodeUpdateValidate updateValidate) {
        FlowNode model = flowNodeMapper.selectOne(
                new QueryWrapper<FlowNode>()
                    .eq("id",  updateValidate.getId())
                    .eq("is_delete", 0)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setId(updateValidate.getId());
        model.setIsValid(updateValidate.getIsValid());
        model.setProposalType(updateValidate.getProposalType());
        model.setIsFlow(updateValidate.getIsFlow());
        model.setIsBackNode(updateValidate.getIsBackNode());
        model.setUserId(updateValidate.getUserId());
        model.setUserName(updateValidate.getUserName());
        model.setUpdateUserId(updateValidate.getUpdateUserId());
        model.setUpdateUserName(updateValidate.getUpdateUserName());
        flowNodeMapper.updateById(model);
    }

    /**
     * 流程节点关联删除
     *
     * @author LikeAdmin
     * @param id 主键ID
     */
    @Override
    public void del(Integer id) {
        FlowNode model = flowNodeMapper.selectOne(
                new QueryWrapper<FlowNode>()
                    .eq("id", id)
                    .eq("is_delete", 0)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setIsDelete(1);
        model.setDeleteTime(String.valueOf(System.currentTimeMillis() / 1000));
        flowNodeMapper.updateById(model);
    }

}
